package com.jxdinfo.hussar.cloud.module.example.dljc.baskethistorycount.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.jxdinfo.hussar.cloud.module.example.dljc.baskethistorycount.dao.BasketHistoryCountMapper;
import com.jxdinfo.hussar.cloud.module.example.dljc.baskethistorycount.model.BasketHistoryCount;
import com.jxdinfo.hussar.cloud.module.example.dljc.baskethistorycount.service.BasketHistoryCountService;
import com.jxdinfo.hussar.cloud.module.example.dljc.baskethistorycount.vo.BasketHistoryCountSearchVO;
import com.jxdinfo.hussar.cloud.module.example.dljc.baskethistorycount.vo.BasketHistoryCountVO;
import com.jxdinfo.hussar.cloud.module.example.tjjc.util.DateHandleUtil;
import com.jxdinfo.hussar.cloud.module.example.tjjc.util.DateUtil;
import com.jxdinfo.hussar.platform.core.base.apiresult.ApiResponse;
import com.jxdinfo.hussar.platform.core.utils.BeanUtil;
import com.jxdinfo.hussar.support.datasource.annotations.HussarDs;
import com.jxdinfo.hussar.support.mp.base.service.impl.HussarServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 吊篮历史统计服务实现类
 */
@Service("dljc.baskethistorycount.BasketHistoryCountServiceImpl")
@HussarDs("master")
public class BasketHistoryCountServiceImpl extends HussarServiceImpl<BasketHistoryCountMapper, BasketHistoryCount> implements BasketHistoryCountService {

    private static final Logger logger = LoggerFactory.getLogger(BasketHistoryCountServiceImpl.class);

    private static final String RETURN_CODE = "0";

    @Autowired
    private BasketHistoryCountMapper basketHistoryCountMapper;

    @Override
    public ApiResponse<List<BasketHistoryCountVO>> findLine(BasketHistoryCountSearchVO basketHistoryCountSearchVO) {
        QueryWrapper<BasketHistoryCount> wrapper = new QueryWrapper<>();

        if(ObjectUtils.isNotEmpty(basketHistoryCountSearchVO.getTyProjectCode())) {
            wrapper.eq("ty_project_code", basketHistoryCountSearchVO.getTyProjectCode());
        }
        if (StringUtils.isNotEmpty(basketHistoryCountSearchVO.getPointIds())) {
            List<String> stringList = Arrays.asList(basketHistoryCountSearchVO.getPointIds().split(","));
            wrapper.in("point_id",stringList);
        }
        if(ObjectUtils.isNotEmpty(basketHistoryCountSearchVO.getCountStartDate())) {
            wrapper.ge("count_date",basketHistoryCountSearchVO.getCountStartDate());
        }
        if(ObjectUtils.isNotEmpty(basketHistoryCountSearchVO.getCountEndDate())) {
            wrapper.le("count_date", basketHistoryCountSearchVO.getCountEndDate());
        }

        wrapper.orderByAsc("count_date");

        List<BasketHistoryCount> list = basketHistoryCountMapper.selectList(wrapper);

        List<BasketHistoryCountVO> dtoList = BeanUtil.copyToList(list, BasketHistoryCountVO.class);
        // 根据日期分组
        Map<String, List<BasketHistoryCountVO>> map = dtoList.stream().collect(Collectors.groupingBy(item-> item.getCountDate().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"))));
        // 补全日期
        List<String> dateList = DateHandleUtil.completionDateList(basketHistoryCountSearchVO.getCountStartDate(), basketHistoryCountSearchVO.getCountEndDate(), map.keySet());

        List<BasketHistoryCountVO> returnlist = new ArrayList<>();

        for (String time : dateList) {

            List<BasketHistoryCountVO> historyCountDtos = map.get(time);

            if (ObjectUtils.isNotEmpty(historyCountDtos)) {
                returnlist.add(historyCountDtos.get(0));
            } else {
                BasketHistoryCountVO basketHistoryCountVO = new BasketHistoryCountVO();
                basketHistoryCountVO.setErrorCount(0);
                basketHistoryCountVO.setCountDate(DateUtil.toLocalDateTime(new Date(time)));
                returnlist.add(basketHistoryCountVO);
            }

        }

        for (BasketHistoryCountVO basketHistoryCountVO : returnlist) {
            basketHistoryCountVO.setTime(DateUtil.toLocalDateTimeString(basketHistoryCountVO.getCountDate()));
        }

        return ApiResponse.success(returnlist);
    }
}
